package com.zy.mc_module.ui.main.forget;

import static com.zy.mc_module.mvp.presenter.LoginPresenter.IMG_CODE;

import android.content.Context;

import com.zy.mc_module.mvp.base.BasePresenter;
import com.zy.mc_module.mvp.bean.BaseBean;
import com.zy.mc_module.mvp.model.GetCodeModel;
import com.zy.mc_module.mvp.model.LoginModel;
import com.zy.mc_module.ui.main.forget.ForgetContract.View;
import com.zy.mc_module.utils.retrofit.RxObserver;
import com.zy.mc_module.utils.retrofit.RxTransform;
import com.zy.mc_module.utils.retrofit.error.ThdException;

import okhttp3.ResponseBody;

/**
 * Created on 2019/01/03
 */
public class ForgetPresenter extends BasePresenter<View> {
    private ForgetModel forgetModel;
    private GetCodeModel getCodeModel;
    private LoginModel loginModel;

    public ForgetPresenter() {
        forgetModel = new ForgetModel();
        getCodeModel = new GetCodeModel();
        loginModel = new LoginModel();
    }

    public void getImgCode(Context context, String tel) {
        loginModel.getImgCode(tel).compose(RxTransform.schedule())
                .subscribe(new RxObserver<ResponseBody>(this, context, true) {
                    @Override
                    protected void onBaseNext(ResponseBody responseBody) {
                        getView().onSuccess(responseBody,IMG_CODE);
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                    }
                });
    }
    /***
     * 获取验证码
     */
    public void getForgetSmsCode(Context context, String tel,String code) {
        getCodeModel.getForgetSmsCode(tel,code).compose(RxTransform.schedule())
                .subscribe(new RxObserver<ResponseBody>(this, context) {
                    @Override
                    protected void onBaseNext(ResponseBody stringBaseBean) {
                        getView().onSuccess(stringBaseBean,12);
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                    }
                });
    }

    /***
     * 忘记密码
     */
    public void forget(Context context, String tel, String code, String pwd) {
        forgetModel.forget(tel, code, pwd).compose(RxTransform.schedule())
                .subscribe(new RxObserver<BaseBean>(this, context, true) {
                    @Override
                    protected void onBaseNext(BaseBean baseBean) {
                        getView().onSuccess();
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                    }
                });
    }
}
